//
//  MySettingViewController.m
//  HuiMWorld
//
//  Created by chirui on 2018/1/8.
//  Copyright © 2018年 Shui. All rights reserved.
//

#import "MySettingViewController.h"
#import "SafetyCenterViewController.h"
#import "FeedBackViewController.h"
#import "UrgentPhoneViewController.h"
#import "AboutUsViewController.h"

@interface MySettingViewController ()<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate>{
    
     UITableView * _tableView;
}

@end

@implementation MySettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
       [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"img_nav_bg"] forBarMetrics:UIBarMetricsDefault];
    self.view.backgroundColor =[UIColor groupTableViewBackgroundColor];
    self.navigationItem.titleView=[TopLabel withLabelWithLabelText:@"设置"];
    
    [self creatTableView];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_nav_renter" target:self action:@selector(backBtnClick)];
}

-(void)backBtnClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)creatTableView {
    
    _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64-48) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    UIView * view =[[UIView alloc]init];
    _tableView.tableFooterView = view;
    [self.view addSubview:_tableView];
    
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return  1;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 50;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
            return 120;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString * cellId = @"cellId";
    
    UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:cellId ];
    if (cell==nil) {
        
        cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
    }else {
        while ([cell.contentView.subviews lastObject] != nil) {
            
            [(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview];
            
        }
    }
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    cell.textLabel.font =[UIFont systemFontOfSize:15];
  
        if (indexPath.row==3) {
            cell.textLabel.text = @"清除缓存";
            double tmpSize = [[SDImageCache sharedImageCache]getSize]/1024/1024;
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2fM",tmpSize];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
 
    if (indexPath.row==0) {
        cell.textLabel.text = @"安全中心";
    }
    if (indexPath.row==1) {
        cell.textLabel.text = @"紧急联系人";
    }
    if (indexPath.row==2) {
  
        cell.textLabel.text = @"意见反馈";
    }
    if (indexPath.row==4) {
        
        cell.textLabel.text = @"关于我们";
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    return cell;
}

-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
        
        UIView * view =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 120)];
        view.backgroundColor =[UIColor clearColor];
        
        UIButton * btn =[UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(40, 20, WIDTH- 80, 80);
        btn.layer.cornerRadius = 20.f;
        btn.layer.masksToBounds = YES;
    [btn setBackgroundImage:ImageName(@"icon_login__buttton_bg") forState:UIControlStateNormal];
        [btn setTitle:@"退出登录" forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(takeOut:) forControlEvents:UIControlEventTouchUpInside];
        
        if (verifTokenAndEnter) {
    
            [view addSubview:btn];
            
            
        }else {

            [btn removeFromSuperview];
        }
    
        
        return view;
 
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row==0) {
        
        SafetyCenterViewController * safe =[[SafetyCenterViewController alloc]init];
        [self.navigationController pushViewController:safe animated:YES];
    }
    if (indexPath.row==1) {
        UrgentPhoneViewController * feedback =[[UrgentPhoneViewController alloc]init];
        [self.navigationController pushViewController:feedback animated:YES];
        
    }
    
    if (indexPath.row==3) {
        
        //清除缓存
        double tmpSize = [[SDImageCache sharedImageCache]getSize]/1024/1024;
        
        UIAlertView * alert =[[UIAlertView alloc]initWithTitle:@"温馨提示" message:[NSString stringWithFormat:@"当前缓存%.2fM",tmpSize] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        alert.tag = 1000;
        [alert show];
    }
   
        if (indexPath.row==2) {
            FeedBackViewController * feedback =[[FeedBackViewController alloc]init];
            [self.navigationController pushViewController:feedback animated:YES];
 
    }
    if (indexPath.row==4) {
        
        AboutUsViewController * us = [AboutUsViewController new];
        [self.navigationController pushViewController:us animated:YES];
    }
    
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (alertView.tag == 1000) {
        
        if (buttonIndex==0) {
            NSLog(@"cennel");
        }else {
            
            [[SDImageCache sharedImageCache]clearDisk];
            [_tableView reloadData];
        }
    }
    
}

-(void)takeOut:(UIButton *)btn {
    
    [self logOutNetWorking];
}

// 退出
-(void)logOutNetWorking{
    
    NSString * parametersStr = [NSString stringWithFormat:@"%@%@",address_url,mySetting_logout_api];
    NSDictionary * parameter = @{@"mid":USER_DEFAULTGet(@"mid")
                                 };

    SHuiMBhubNoTimeStr(@"正在加载...");
    [PostNetWorking postNetWorkingStr:parametersStr  andparameters:parameter  Success:^(id responseObject) {
        SHuiMBHidleYes;
        if ([responseObject[@"code"] intValue]==200) {
            SHuiMBhudTextTimeStr(@"退出成功");
            dispatch_async(dispatch_get_global_queue(0, 0), ^{

                USER_DEFAULTSet(@"", @"token");
                USER_DEFAULTSet(@"", @"enter");
                USER_DEFAULTSet(@"", @"tokenVerify");
                USER_DEFAULTSet(@"", @"mid");
                USER_DEFAULTSet(@"", @"pay_pwd");

                dispatch_after(dispatch_time(DISPATCH_TIME_NOW , (1 * NSEC_PER_SEC)), dispatch_get_main_queue(),^{

                    [self.navigationController popViewControllerAnimated:YES];
                });
            });


        } else {

            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }

    } failed:^(NSError *error) {

        SHuiMBHidleYes;
        SHuiMBhudTextTimeStr(@"请检查您的网络!");
    }];
}



@end
